package com.geek.javase.ch18.work.work3;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.stream.Stream;

/*
 *@Date 2025/11/7
 *@Author wangpeng
 *@Description
 * 自定义注解，模仿@Test注解
 */
public class MainClass {
    @MyTest
    public void test1(){
        System.out.println("---------test1()----------");
    }
    public void test2(){
        System.out.println("---------test2()----------");
    }
    @MyTest
    public void test3(){
        System.out.println("---------test3()----------");
    }
   /* @MyTest
    public void test4(){
        System.out.println("---------test4()----------");
    }*/
    //私有的也可以
   @MyTest
   private void test4(){
       System.out.println("---------test4()----------");
   }
    public static void main(String[] args) {
       //执行对象
        MainClass mainClass = new MainClass();
        //1.获取类类型
        Class<MainClass> clazz = MainClass.class;
        //2.获取类中所有方法对象
        Method[] methods = clazz.getDeclaredMethods();
        //3.迭代判断是否有@MyTest注解标注
        Stream.of(methods).forEach(method -> {
            if (method.isAnnotationPresent(MyTest.class)) {
                //取消访问权限检查
                method.setAccessible(true);
                //4.执行方法
                try {
                    method.invoke(mainClass);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
}
